
/**
 * 随机生成几位字符
 * @param strlen 
 * @returns 
 */

export const getRandomStr = (length = 6): string => {
    const numbers = '0123456789';
    let result = '';

    const values = new Uint32Array(length);
    window.crypto.getRandomValues(values);

    for(let i = 0; i < length; i++) {
      result += numbers[values[i] % numbers.length];
    }

    return result;
}
//随机数
export const getRandomValues = () => {
  let cryptoKeys = ['webkitCrypto','mozCrypto','oCrypto','msCrypto'];
  let crypto = window.crypto;
  if(!crypto){
    for(let i = 0;i<cryptoKeys.length;i++){
      if(cryptoKeys[i]){
        crypto = window[cryptoKeys[i]]
        break
      }
    }
  }
  let randomBuffer = new Uint32Array(1);
  return crypto.getRandomValues(randomBuffer)[0] / Math.pow(2, 32);
}
//随机不重复id
export const randomRangeId = (num:any=10)=>{
  let returnStr = "",       
      charStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; 
  for(let i=0; i<num; i++){
      let index = Math.round(getRandomValues() * (charStr.length-1));
      returnStr += charStr.substring(index,index+1);
  }
  return returnStr;
}
//获取url参数
export const getQueryString = (name:any)=> {
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  if(window.location.hash.indexOf("?") < 0){
    return null;
  }
  let r = window.location.hash.split("?")[1].match(reg);
  if (r != null) return decodeURIComponent(r[2]); 
}